package ex.datastream.windows.windowAssigners;

import ex.datastream.ApiFrame;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.windowing.assigners.ProcessingTimeSessionWindows;
import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

/**
 * 根据不同的业务场景，Time Window也可以分为三种类型，分别是滚动窗口(Tumbling Window)、滑动窗口（Sliding Window）和会话窗口（Session Window）
 */
public class TimeWindows extends ApiFrame {
    public static void main(String[] args) throws Exception {
        TimeWindows windows=new TimeWindows();
        windows.getEnv();

        SingleOutputStreamOperator<Tuple2<String,Long>> outputStreamOperator =windows.getSingleOutputStreamOperator();
        KeyedStream<Tuple2<String,Long>, String> keyedStream = outputStreamOperator.keyBy(value -> value.f0);


        WindowedStream<Tuple2<String,Long>, String, TimeWindow> windowedStream1= keyedStream.window(TumblingProcessingTimeWindows.of(Time.seconds(5)));
        WindowedStream<Tuple2<String,Long>, String,TimeWindow> windowedStream2 = keyedStream.window(SlidingProcessingTimeWindows.of(Time.seconds(10),Time.seconds(5)));
        WindowedStream<Tuple2<String,Long>, String, TimeWindow> windowedStream3 = keyedStream.window(ProcessingTimeSessionWindows.withGap(Time.seconds(5)));
        windowedStream1.sum(1).print();

        windows.env.execute();
    }
}
